local skel = fk.CreateSkill{
  name = "lb__yanxir",
  tags = {Skill.Compulsory}
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable{
  ["lb__yanxir"] = "演析",
  [":lb__yanxir"] = "<b>锁定技</b>，你仅可以使用本回合进入弃牌堆中的最大类别的牌且不可被响应。",

  ["$lb__yanxir1"] = "歼灭开始。",
  ["$lb__yanxir2"] = "命令执行。",
}

local function findKeysOfMaxValue(tbl)
    if not tbl or next(tbl) == nil then
        return {}
    end
    
    local max_value = nil
    for _, value in pairs(tbl) do
        if max_value == nil or value > max_value then
            max_value = value
        end
    end
    
    local result = {}
    for key, value in pairs(tbl) do
        if value == max_value then
            table.insert(result, key)
        end
    end
    
    return result
end

skel:addEffect(fk.PreCardUse, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player then
      local type_list = {}
      for _, ty in ipairs({Card.TypeBasic, Card.TypeEquip, Card.TypeTrick}) do
        local cards = table.filter(DIY.getCenterCards(Fk:currentRoom()), function (id)
          return Fk:getCardById(id).type == ty
        end)
        type_list[ty] = #cards
      end
      return table.contains(findKeysOfMaxValue(type_list), data.card.type)
    end
  end,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = player.room.players
  end,
})

skel:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if player:hasSkill(skel.name) then
      local type_list = {}
      for _, ty in ipairs({Card.TypeBasic, Card.TypeEquip, Card.TypeTrick}) do
        local cards = table.filter(DIY.getCenterCards(Fk:currentRoom()), function (id)
          return Fk:getCardById(id).type == ty
        end)
        type_list[ty] = #cards
      end
      return not table.contains(findKeysOfMaxValue(type_list), card.type)
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return skel